package com.hzhr.iot.comm.listener;

import com.hzhr.iot.comm.Global;
import com.hzhr.iot.comm.model.HttpResult;
import com.hzhr.iot.comm.model.OcDeviceOptModel;
import com.hzhr.iot.comm.service.HttpAPIService;
import com.hzhr.iot.core.util.HzhrJsonUtil;
import com.hzhr.iot.huaweiiot.sdk.model.device.addDevice.AddDeviceModel;
import com.hzhr.iot.huaweiiot.sdk.model.device.addDevice.AddDeviceResponse;
import com.hzhr.iot.huaweiiot.sdk.model.device.deleteDevice.DeleteDeviceModel;
import com.hzhr.iot.huaweiiot.sdk.model.device.deleteDevice.DeleteDeviceResponse;
import com.hzhr.iot.huaweiiot.sdk.model.device.editDevice.EditDeviceModel;
import com.hzhr.iot.huaweiiot.sdk.model.device.editDevice.EditDeviceResponse;
import com.hzhr.iot.huaweiiot.sdk.service.Huaweiiot_DeviceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.Map;

@Component
public class OcDeviceListener implements CommandLineRunner {
    @Autowired
    HttpAPIService httpAPIService;
    @Value("${imeiRootUri}")
    String imeiRootUri;
    @Autowired
    Global global;
    @Autowired
    Huaweiiot_DeviceService huaweiiot_deviceService;

    @Override
    public void run(String... strings) throws Exception {
        if (StringUtils.isEmpty(imeiRootUri)) return;
        while (true) {
            try {
                //查询oc设备操作
                HttpResult result = httpAPIService.doPost(imeiRootUri + "/oc-device-opt/getFirstNormalOpt");
                int code = result.getCode();
                if (code != 200) continue;
                String json = result.getBody();
                OcDeviceOptModel ocDeviceOptModel = null;
                try {
                    ocDeviceOptModel = HzhrJsonUtil.json2object(json, OcDeviceOptModel.class);
                } catch (Throwable e) {

                }
                if (ocDeviceOptModel == null) continue;

                int opt = ocDeviceOptModel.getOpt();
                if (opt == 1) {
                    //在oc上添加设备
                    AddDeviceResponse response = null;
                    try {
                        Map<String, String> header = new HashMap<>();
                        Map<String, Object> query = new HashMap<>();
                        String token = global.getFirstToken();
                        header.put("token", token);
                        AddDeviceModel addDeviceModel = new AddDeviceModel();
                        addDeviceModel.setDeviceName(ocDeviceOptModel.getDes());
                        addDeviceModel.setImei(ocDeviceOptModel.getImei());
                        response = huaweiiot_deviceService.addDevice(header, query, addDeviceModel);
                    } catch (Throwable e) {

                    }

                    if (response == null) {
                        //如果添加不成功，将oc设备操作的状态置为不正常
                        Map<String, Object> map = new HashMap<>();
                        map.put("id", ocDeviceOptModel.getId());
                        map.put("status", "2");
                        httpAPIService.doPost(imeiRootUri + "/oc-device-opt/updateOpt", map);
                    } else {
                        //如果添加成功则删除该oc设备操作，并且更新对应设备的平台id
                        Map<String, Object> map = new HashMap<>();
                        map.put("id", ocDeviceOptModel.getId());
                        httpAPIService.doPost(imeiRootUri + "/oc-device-opt/delOpt", map);

                        Map<String, Object> map1 = new HashMap<>();
                        map1.put("imei", ocDeviceOptModel.getImei());
                        map1.put("iotPlatformDeviceId", response.getDeviceId());
                        httpAPIService.doPost(imeiRootUri + "/iot-device/updateIotPlatformDeviceId", map1);
                    }
                } else if (opt == 2) {
                    //在oc上删除设备
                    DeleteDeviceResponse response = null;
                    try {
                        Map<String, String> header = new HashMap<>();
                        Map<String, Object> query = new HashMap<>();
                        String token = global.getFirstToken();
                        header.put("token", token);
                        DeleteDeviceModel deleteDeviceModel = new DeleteDeviceModel();
                        deleteDeviceModel.setDeviceId(ocDeviceOptModel.getIotPlatformDeviceId());
                        response = huaweiiot_deviceService.deleteDevice(header, query, deleteDeviceModel);
                    } catch (Throwable e) {

                    }

                    if (response == null) {
                        //如果添加不成功，将oc设备操作状态置为不正常
                        Map<String, Object> map = new HashMap<>();
                        map.put("id", ocDeviceOptModel.getId());
                        map.put("status", "2");
                        httpAPIService.doPost(imeiRootUri + "/oc-device-opt/updateOpt", map);
                    } else {
                        //如果添加成功则删除该oc设备操作
                        Map<String, Object> map = new HashMap<>();
                        map.put("id", ocDeviceOptModel.getId());
                        httpAPIService.doPost(imeiRootUri + "/oc-device-opt/delOpt", map);
                    }
                } else if (opt == 3) {
                    //在oc上修改设备
                    EditDeviceResponse response = null;
                    try {
                        Map<String, String> header = new HashMap<>();
                        Map<String, Object> query = new HashMap<>();
                        String token = global.getFirstToken();
                        header.put("token", token);
                        EditDeviceModel  editDeviceModel = new EditDeviceModel();
                        editDeviceModel.setDeviceId(ocDeviceOptModel.getIotPlatformDeviceId());
                        editDeviceModel.setDeviceName(ocDeviceOptModel.getDes());
                        response = huaweiiot_deviceService.editDevice(header, query, editDeviceModel);
                    } catch (Throwable e) {

                    }

                    if (response == null) {
                        //如果添加不成功，将oc设备操作状态置为不正常
                        Map<String, Object> map = new HashMap<>();
                        map.put("id", ocDeviceOptModel.getId());
                        map.put("status", "2");
                        httpAPIService.doPost(imeiRootUri + "/oc-device-opt/updateOpt", map);
                    } else {
                        //如果添加成功则删除该oc设备操作
                        Map<String, Object> map = new HashMap<>();
                        map.put("id", ocDeviceOptModel.getId());
                        httpAPIService.doPost(imeiRootUri + "/oc-device-opt/delOpt", map);
                    }
                }
            } catch (Throwable e) {

            }
            Thread.sleep(1000);
        }
    }

}
